package org.jf.tool.fx.jftoolfx.tools.cron.controller;

import com.alibaba.fastjson2.util.DateUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Spinner;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import org.jf.tool.fx.jftoolfx.util.CronUtil;

import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

/**
 * cron 工具
 *
 * @author jifeng_zhang
 * @date 2025/10/9 上午11:41
 */
public class ValidateController extends CronController {

    @FXML
    protected TextField cronText;
    @FXML
    protected TextArea resultText;
    private CronController cronController;

    public void setCronController(CronController cronController) {
        this.cronController = cronController;
    }

    @Override
    protected void init(URL url, ResourceBundle resourceBundle) {

    }

    public void run(ActionEvent actionEvent) {
        String cron = cronText.getText();
        // cron 校验
        if (cron == null || cron.isEmpty()) {
            resultText.setText("请输入cron表达式");
        }
        int count = 5;
        String runCount = runCountText.getText();
        try {
            count = Integer.parseInt(runCount);
            if (count < 1 || count > 100) {
                count = 5;
            }
        } catch (NumberFormatException e) {
            // ignore
        }
        runCountText.setText(String.valueOf(count));
        try {
            List<Date> nextExecutionTimes = CronUtil.getNextExecutionTimes(cron, count);
            resultText.setText("");
            for (Date nextExecutionTime : nextExecutionTimes) {
                resultText.appendText(DateUtils.formatYMDHMS19(nextExecutionTime) + "        ");
            }
        } catch (Exception e) {
            resultText.setText(e.getMessage());
        }
    }

    public void copy(ActionEvent actionEvent) {
        copy(cronText.getText());
    }

}
